<?php
class Article_Data_Articlelist {
	
	private $database;
	private $comments;
	
	public function __construct(){
		$this->database = new Core_Driver_MysqlImproved();
		$this->comments = new Article_Data_Comments();
	}
	
	public function getArticlesListNoCategory(){
		
		$article = array();
		
		//$article = $this->getCategorySettings();
		
		$this->database->connect();
		$this->database->prepare(
			"
				SELECT 
				*
				FROM 
				page_settings,
				page_meta_values,
				page_content_values
				WHERE
				page_settings.page_name = page_meta_values.page_name
				AND
				page_settings.page_name = page_content_values.page_name
				ORDER
				BY
				page_content_values.page_creation_date
				DESC
				;
				"
		);
		$this->database->query();
		$article['content'] = '';
		while($articleData = $this->database->fetch('array')){
			$article['content'] .= '
					<li class="art-list-item">
        			<div class="art-list-item-title-and-time">
          			<h2 class="art-list-title">
            		<a href="'.SERVER_ROOT.'?article&page='.$articleData['page_name'].'">'.$articleData['page_title'].'</a>
          			</h2>
          			<div class="art-list-time">'.$articleData['page_creation_date'].'</div>
        			</div>
        			<p>'.$articleData['page_description'].'</p>
        			</a>
      				</li>
					';
		}	
		return $article;
	}
	
	public function getArticlesList($category){
	
		$article = array();
	
		$article = $this->getCategorySettings($category);
	
		$this->database->connect();
		$this->database->prepare(
				"
				SELECT
				*
				FROM
				page_settings,
				page_meta_values,
				page_content_values
				WHERE
				page_settings.page_name = page_meta_values.page_name
				AND
				page_settings.page_name = page_content_values.page_name
				AND
				page_content.category = '$category'
				ORDER
				BY
				page_content_values.page_creation_date
				DESC
				;
				"
		);
		$this->database->query();
		$article['content'] = '';
		while($articleData = $this->database->fetch('array')){
			$article['content'] .= '
					<li class="art-list-item">
        			<div class="art-list-item-title-and-time">
          			<h2 class="art-list-title">
            		<a href="'.SITE_ROOT.'?article&page='.$articleData['page_name'].'">'.$articleData['page_title'].'</a>
          			</h2>
          			<div class="art-list-time">'.$articleData['page_creation_date'].'</div>
        			</div>
        			<p>'.$articleData['page_description'].'</p>
        			</a>
      				</li>
					';
		}
		return $article;
	}
	
	private function getCategorySettings($category){
		
		$categoryData = array();
		
		$this->database->connect();
		$category = $this->database->escape($category);
		$this->database->prepare(
			"
				select 
				*
				from 
				category_settings,
				category_meta_values
				where
				category_settings.category_name = category_meta_values.category_name
				AND
				category_settings.category_name = '$category'
				LIMIT
				1
				;
				"
		);
		$this->database->query();
		$categoryData = $this->database->fetch('array');
		return $categoryData;
	}
}